#include <jni.h>
#include <string>
#include <GLES2/gl2.h>


extern "C" JNIEXPORT jstring JNICALL
Java_com_example_triangle_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
const char vertexShaderCode[] =
"attribute vec4 vPosition;\n"
"void main() {\n"
"  gl_Position = vPosition;\n"
"}";

// 片段着色器代码，用于控制三角形的颜色
const char fragmentShaderCode[] =
"precision mediump float;\n"
"uniform vec4 uColor;\n"
"void main() {\n"
"  gl_FragColor = uColor;\n"
"}";
GLint program;

GLuint CompileShader(int type, const char *shaderCode) {
    GLint shader = glCreateShader(type);
    if (shader != 0) {
        glShaderSource(shader, 1, &shaderCode, NULL);
        glCompileShader(shader);
        //检测状态
        GLint glResult = GL_FALSE;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &glResult);
        if (glResult == GL_FALSE) {
            //创建失败
            glDeleteShader(shader);
            return 0;
        }
        return shader;
    }
    //创建失败
    return 0;
}
GLint LinkProgram(GLint vertexShader, GLint fragmentShader) {
    GLint program = glCreateProgram();
    if (program != 0) {
        //将顶点着色器加入到程序
        glAttachShader(program, vertexShader);
        //将片元着色器加入到程序中
        glAttachShader(program, fragmentShader);
        //链接着色器程序
        glLinkProgram(program);
        GLint linkStatus = GL_FALSE;
        glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
        if (linkStatus == GL_FALSE) {
            glDeleteProgram(program);
            return 0;
        }
        return program;
    }
    //创建失败
    return 0;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_triangle_MyGLRenderer_drawTriangle(JNIEnv *env, jobject thiz) {
    // 设置三角形形状
    GLfloat triangleVertices[] = {
            0.0f,  0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f
    };
    float color[] = {
            1.0f, 0.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 1.0f
    };

//把颜色缓冲区设置为我们预设的颜色
    glClear(GL_COLOR_BUFFER_BIT);
    //准备坐标数据
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices);
    //启用顶点的句柄
    glEnableVertexAttribArray(0);

    //绘制三角形颜色
    glEnableVertexAttribArray(1);
    GLint colorHandle=glGetUniformLocation(program, "uColor");
    glUniform4fv(colorHandle, 1, color);

    //绘制
    glDrawArrays(GL_TRIANGLES, 0, 3);
    //禁止顶点数组的句柄
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_triangle_MyGLRenderer_init(JNIEnv *env, jobject thiz) {
    //创建顶点着色器
    GLint vertexShader = CompileShader(GL_VERTEX_SHADER, vertexShaderCode);
    if (vertexShader == 0) {

        return;
    }
    //创建片段着色器
    GLint fragmentShader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderCode);
    if (fragmentShader == 0) {
        return;
    }
    program = LinkProgram(vertexShader, fragmentShader);
    //在OpenGLES环境中使用程序片段
    glUseProgram(program);

}

